65. Introduction to Psake

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

65.1: Basic outline

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Task Rebuild -Depends Clean, Build {
"Rebuild"
}

Task Build {
"Build"
}

Task Clean {
"Clean"
}
1
Task default -Depends Build

65.2: FormatTaskName example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Will display task as:
# -------- Rebuild --------
# -------- Build --------
FormatTaskName "-------- {0} --------"

# will display tasks in yellow colour:
# Running Rebuild
FormatTaskName {
  param($taskName)
  "Running $taskName" - foregroundcolor yellow
}

Task Rebuild -Depends Clean, Build {
  "Rebuild"
}

Task Build {
  "Build"
}
1
2
3
4
5
Task Clean {
"Clean"
}

Task default -Depends Build

65.3: Run Task conditionally

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
propreties {
  $isOk = $false
}

# By default the Build task won't run, unless there is a param $true
Task Build -precondition { return $isOk } {
  "Build"
}

Task Clean {
  "Clean"
}

Task default -Depends Build

65.4: ContinueOnError

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Task Build -depends Clean {
  "Build"
}

Task Clean -ContinueOnError {
  "Clean"
  throw "throw on purpose, but the task will continue to run"
}

Task default -Depends Build